//
//  VillageDetailsViewController.m
//  Village
//
//  Created by 程虎 on 2017/9/14.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "VillageDetailsViewController.h"
#import "LegalPersonTableViewCell.h"
#import "YZDisplayViewHeader.h"
#import "VillageDetailsCell1.h"
#import "VillageDetailsCell.h"
#import "VillageDetailsModel.h"
#import "VillageHttpTools.h"
#import "RequesCover.h"
#import "Legalinfo.h"
#import "OtherInfo.h"
#import "Villages.h"
#import "Persons.h"
#import "Orginfo.h"
#import "ShowEmptyView.h"
@interface VillageDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, weak) RequesCover *cover;
@property (nonatomic, strong) NSMutableArray *dataAry;
@property (nonatomic, strong) UITableView *tabView;
@end

@implementation VillageDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - kNavHeight - 44.5) style:UITableViewStylePlain];
    self.tabView.delegate = self;
    self.tabView.dataSource = self;
    self.tabView.showsVerticalScrollIndicator = NO;
    self.tabView.showsHorizontalScrollIndicator = NO;
    self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tabView.backgroundColor = [Util colorWithHexString:@"f7f7f7"];
    [self.view addSubview:self.tabView];
    
    if (@available(iOS 11.0, *)) {
        self.tabView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self.tabView registerNib:[UINib nibWithNibName:@"VillageDetailsCell" bundle:nil] forCellReuseIdentifier:@"VillageDetailsCell"];
    [self.tabView registerNib:[UINib nibWithNibName:@"VillageDetailsCell1" bundle:nil] forCellReuseIdentifier:@"VillageDetailsCell1"];
    [self.tabView registerClass:[LegalPersonTableViewCell class] forCellReuseIdentifier:@"LegalPersonTableViewCell"];
    /****滚动完成请求数据*******/
    // 如果想要滚动完成或者标题点击的时候，加载数据，需要监听通知
    // 监听滚动完成或者点击标题，只要滚动完成，当前控制器就会发出通知
    // 只需要监听自己发出的，不需要监听所有对象发出的通知，否则会导致一个控制器发出，所有控制器都能监听,造成所有控制器请求数据
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:YZDisplayViewClickOrScrollDidFinshNote object:self];
    // 开发中可以搞个蒙版，一开始遮住当前界面，等请求成功，在把蒙版隐藏.
    RequesCover *cover = [RequesCover requestCover];
    
    [self.view addSubview:cover];
    
    _cover = cover;
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.cover.frame = self.view.bounds;
}

#pragma mark - TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.types == 0 ? 1 : [(NSArray *)self.dataAry[self.types] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arrays = self.dataAry[self.types];
    if (self.types == 0) {
        VillageDetailsCell *cell = [VillageDetailsCell cellWithTableView:tableView];
        cell.model = self.dataAry[self.types];
        return cell;
    }
    else if (self.types != 3)
    {
        VillageDetailsCell1 *cell = [VillageDetailsCell1 cellWithTableView:tableView];
        cell.model = arrays[indexPath.row];
        if (self.types != 1) {
            cell.desLab.textColor = [Util colorWithHexString:@"#333333"];
            cell.contentLab.textAlignment = NSTextAlignmentRight;
            cell.contentLab.textColor = [Util colorWithHexString:@"#999999"];
        }
        return cell;
    }
    else {
        Persons *model = arrays[indexPath.row];
        LegalPersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LegalPersonTableViewCell"];
        cell.leftiamge.image = [UIImage imageNamed:@"default_img"];
        cell.namelable.text = model.name;
        cell.messagelable.text = [NSString stringWithFormat:@"%@ %@ %@",model.sexcn,model.nationcn,model.educationcn];
        cell.phonelable.text = model.phone;
        cell.personlable.text = model.cardno;
        cell.rightimage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@query",model.persontype]];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Class currentClass = [VillageDetailsCell class];
    if (self.types == 0) {
        return [self.tabView cellHeightForIndexPath:indexPath model:self.dataAry[self.types] keyPath:@"model" cellClass:currentClass contentViewWidth:SCREEN_W];
    }
    else if (self.types != 3) {
        Class currentClass = [VillageDetailsCell1 class];
        NSArray *arrays = self.dataAry[self.types];
        return [self.tabView cellHeightForIndexPath:indexPath model:arrays[indexPath.row] keyPath:@"model" cellClass:currentClass contentViewWidth:SCREEN_W];
    }
    else {
        return 110;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *views = [UIView new];
    views.backgroundColor = [Util colorWithHexString:@"#f9f9f9"];
    return views;
}

#pragma mark - 加载数据
- (void)loadData
{
    [VillageHttpTools GetVillageInfoSuccess:^(NSArray *valueAry) {
        self.dataAry = [NSMutableArray arrayWithArray:valueAry];
        [self.cover removeFromSuperview];
        [EmptyFunction hideEmpty:self.tabView];
        [self.tabView reloadData];
    } failure:^(NSString *error) {
        [self.cover removeFromSuperview];
         if (self.types == 0) {
             self.tabView.hidden = YES;
             ShowEmptyView *show = [[ShowEmptyView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, KDeviceHeight-100*kScreenScaleY)];
             [self.view addSubview:show];
         }else{
              [EmptyFunction showEmpty:self.tabView Height: self.tabView.frame.size.height - self.tabView.tableHeaderView.height];
         }
       
    }];
}

@end
